package com.zaofan.service;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;

import com.zaofan.bean.Dynamic;
import com.zaofan.bean.DynamicReply;
import com.zaofan.bean.DynamicReplyDTO;
import com.zaofan.dao.DynamicDao;
import com.zaofan.dao.DynamicReplyDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.C3p0DataSourceUtils;
import com.zaofan.utils.SqlBuilderUtils;

public class DynamicReplyService {

	private DynamicReplyDao dynamicReplyDao = new DynamicReplyDao();

	private DynamicDao dynamicService = new DynamicDao();

	public void add(DynamicReply dynamicReply) {
		dynamicReplyDao.add(dynamicReply);
	}

	public void del(DynamicReply dynamicReply) {
		dynamicReplyDao.del(dynamicReply);
	}

	public Long count(Map<String, Object> params) {
		return dynamicReplyDao.count(params);
	}

	public DynamicReply findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return dynamicReplyDao.findOne(params);
	}

	public List<DynamicReplyDTO> findByDynamicIdWithOther(String begin, String end, String dynamicId, String replyType,
			String state) {
		List<DynamicReplyDTO> replys = dynamicReplyDao.findByDynamicIdWithOther(begin, end, dynamicId, replyType,
				state);
		return replys;
	}

	public Long countByDynamicIdWithOther(String dynamicId, String replyType, String state) {
		return dynamicReplyDao.countByDynamicIdWithOther(dynamicId, replyType, state);
	}

	public void update(DynamicReply dynamicReply) {
		dynamicReplyDao.update(dynamicReply);

	}

	public void delReply(DynamicReply dynamicReply) throws Exception {
		Dynamic dynamic = dynamicService.findById(dynamicReply.getReplyId());
		if (dynamic == null) {
			throw new ServiceException("参数错误");
		}
		try {
			Connection connection = C3p0DataSourceUtils.getConnection();
			QueryRunner runner = new QueryRunner();
			String sql = "";
			try {
				connection.setAutoCommit(false);
				if (dynamicReply.getReplyType() == 0) {
					dynamic.setPraise(dynamic.getPraise() - 1);
				} else {
					dynamic.setCount(dynamic.getCount() - 1);
				}
				// 更新 话题的 相关数量
				sql = SqlBuilderUtils.builderUpdateSql(Dynamic.class, dynamic);
				System.err.println(sql);
				runner.update(connection, sql);
				sql = SqlBuilderUtils.builderDelSql(DynamicReply.class, dynamicReply);
				System.err.println(sql);
				runner.update(connection, sql);
				connection.commit();
			} catch (Exception e) {
				connection.rollback();
				e.printStackTrace();
				throw e;
			} finally {
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}

	}

	public DynamicReply findPraiseReplyByUserIdAndReplyId(Integer userId, Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId + "");
		params.put("replyId", id + "");
		params.put("replyType", 0 + "");
		DynamicReply reply = this.dynamicReplyDao.findOne(params);
		return reply;
	}

	
	public Long countUnRead(Integer userId,Integer replyType){
		return dynamicReplyDao.countUnRead(userId,replyType);
	}

	public void readReply(Integer id) {
		DynamicReply reply = this.findById(id);
		reply.setState(1);
		this.update(reply);
	}

	public List<DynamicReplyDTO> findByUserIdWithOther(String begin, String end, String dynamicId, String replyType,
			String state) {
		return dynamicReplyDao.findByUserIdWithOther(begin, end, dynamicId, replyType, state);
	}

}
